<?php
namespace app\model;

use think\facade\Cache;
use think\Model;

class BocSeo extends Model
{

    protected $table = 'boc_seo';

    /**
     * 查询seo.
     */
    public function get_seo($type)
    {

        $key = 'seo_'.$type;
        $pre = Cache::get($key);
        if(empty($pre)){
            $pre = $this->field('title,description,keywords,type')->where('type',$type)->find();
            Cache::set($key,$pre);
        }

        return $pre ? $pre->toarray() : ['title' => '', 'description' => '', 'keywords' => ''];
    }

    /**
     * 查询seo.
     */
    public function get_info($c, $a = '')
    {
        $key = 'seo-'.$c.'-'.$a;
        $cache_tag = 'seo';
        $info = Cache::get($key);
        if(empty($info)) {
            $Boc_menu = new BocMenu();
            $p = $Boc_menu->field('id')->where(['pid'=>0,'key'=>$c,'status'=>1,'is_deleted'=>0])->findOrEmpty()->toArray();
            //没有子集，使用父级seo
            if(empty($a) || $a == 'index'){
                $menu_id = $p['id'];
            }
            $c = $Boc_menu->field('id')->where(['pid'=>$p['id'],'key'=>$a,'status'=>1,'is_deleted'=>0])->findOrEmpty()->toArray();
            if(empty($c)){
                //子集没有设置seo，使用父级seo
                $menu_id = $p['id'];
            }else{
                //获取子集seo
                $menu_id = $c['id'];
            }

            $info = $this->field('title,keywords,description')->where(['menu_id'=>$menu_id])->findOrEmpty()->toArray();
            Cache::tag($cache_tag)->set($key, $info);
        }
        return $info;
    }
}